/* -----------------------------------------------------------------------
 * Include the following lines in your makefile:
 *
 * unittest3: unittest4.c dominion.o rngs.o
 *      gcc -o unittest4 -g  unittest4.c dominion.o rngs.o $(CFLAGS)
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

// set USE_ASSERT to 1 to include the assert statement
#define USE_ASSERT 0

int main() {
    int i, r, p, player, deckCount, similarity;
    int s, sim_avg, sim_max, sim_min, shuffles = 10;
    int oldDeck[MAX_DECK];
    int seed = 1000;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

	printf ("TESTING shuffle():\n");
 
	for (p = 2; p <= MAX_PLAYERS; p++)
    {
#if (NOISY_TEST == 1)
    	printf("Test shuffle for %d players.\n", p);
#endif
    	memset(&G, 23, sizeof(struct gameState));   // clear the game state
		r = initializeGame(p, k, seed, &G); // initialize a new game
    	for (player = 0; player < p; player++)
    	{
#if (NOISY_TEST == 1)
			printf("Shuffle for player %d.\n", player);
#endif
			deckCount = G.deckCount[player];
			
			// players first five cards are in player's hand, not in deck.
			for (i = 0; i < deckCount; i++)
			{
				oldDeck[i] = G.deck[player][i];
#if (NOISY_TEST == 1)
				//printf("Card %d is %d.\n", i, oldDeck[i]);
#endif
			}
			
			sim_avg = 0;
			sim_max = 0; 
			sim_min = deckCount;
			
			// shuffle the cards
			for (s = 0; s < shuffles; s++){
				r = shuffle(player,&G);
				
				similarity = 0;
				for (i = 0; i < deckCount; i++)
				{
#if (NOISY_TEST == 1)
					//printf("Card %d is %d.\n", i, G.deck[player][i]);
#endif
					if (oldDeck[i] == G.deck[player][i])
						similarity++;
				}
				
				sim_max = similarity > sim_max ? similarity : sim_max;
				sim_min = similarity < sim_min ? similarity : sim_min;
				sim_avg += similarity;
			}
			
			sim_avg /= deckCount;
			
#if (NOISY_TEST == 1)
	       	printf("Deck size = %d, avg similarity = %d, min similarity = %d, max similarity = %d.\n", deckCount, sim_avg, sim_min, sim_max);
#endif
						
#if (USE_ASSERT == 1)			
			//check against the average similarity to allow for some similarity between shuffles.
    	   	assert(sim_avg < deckCount); // check how shuffled cards are.
#endif
		}
	}
	
#if (USE_ASSERT == 0)			
			printf("Did not test assert that average similarity\nbetween shuffled hands is less than the deck count.\n");
#endif

    printf("All domain tests passed!\n");
            
    return 0;
}
